package cn.tedu.oa_system.service.impl;

import cn.tedu.oa_system.base.json_result.ResultDataDto;
import cn.tedu.oa_system.base.json_result.ResultDto;
import cn.tedu.oa_system.base.util.SessionUtil;
import cn.tedu.oa_system.mapper.DeptMapper;
import cn.tedu.oa_system.pojo.Dept;
import cn.tedu.oa_system.pojo.in.DeptIn;
import cn.tedu.oa_system.service.DeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

import static cn.tedu.oa_system.base.constant.DataBaseConstants.DATABASE_AFFECTED_ROW;

/**
 * @Author MW
 * @Description
 * @CreateDate 2022/12/28
 */
@Slf4j
@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private SessionUtil sessionUtil;

    @Override
    public ResultDataDto<Dept> list() {
        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Dept::getDeptId,-1);
        List<Dept> depts = deptMapper.selectList(queryWrapper);
        ResultDataDto<Dept> resultDataDto = new ResultDataDto<>();
        resultDataDto.setDataList(depts);
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }

    @Override
    public ResultDto<Void> addNewDept(DeptIn deptIn) {
        log.info("正在尝试添加的部门信息：[{}]",deptIn);
        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dept::getDeptName,deptIn.getDeptName());
        Integer i = deptMapper.selectCount(queryWrapper);
        Assert.isTrue(i==DATABASE_AFFECTED_ROW,"该部门已存在,请稍后重试");
        Dept dept=new Dept();
        BeanUtils.copyProperties(deptIn,dept);
        dept.setStartTime(new Date());
        int row = deptMapper.insert(dept);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"新增部门失败，请稍后重试");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("添加部门成功");
        return resultDto;
    }

    @Override
    public ResultDto<Void> upDateDept(DeptIn deptIn) {
        log.info("正在尝试更改的部门信息：[{}]",deptIn);
        Dept dept=new Dept();
        BeanUtils.copyProperties(deptIn,dept);
        dept.setStartTime(new Date());
        int row = deptMapper.updateById(dept);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"修改部门失败，请稍后重试");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("修改部门成功");
        return resultDto;
    }

    @Override
    public ResultDto<Void> deleteDept(Long deptId) {
        log.info("正在尝试删除的部门id：[{}]",deptId);
        LambdaQueryWrapper<Dept> Wrapper=new LambdaQueryWrapper<>();
        Wrapper.eq(Dept::getDeptId,deptId);
        int row = deptMapper.delete(Wrapper);
        /*int row = deptMapper.deleteById(deptId);*/
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"删除部门失败,请稍后重试");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("删除部门成功");
        return resultDto;
    }
}
